"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具清理dss内部的锁资源
Create At   : 2023/4/20
Owner       : opentestcase019
Description :
    1.清理dss内部的锁资源，不指定DSS_HOME参数
    2.清理dss内部的锁资源，指定DSS_HOME参数为正常值
    3.清理dss内部的锁资源，指定错误的DSS_HOME参数
    4.清理dss内部的锁资源，指定权限不足的DSS_HOME参数
    5.在备机点执行清理dss内部的锁资源
    6.在dss关闭的情况下执行
    7.在集群停止的情况下执行
    8.DSS_HOME路径权限不足时，清理dss内部的锁资源，不指定DSS_HOME参数
Expect      :
    1.清理dss内部的锁资源成功
    2.清理dss内部的锁资源成功
    3.合理报错Failed to get vg entry info
    4.合理报错not exist
    5.清理dss内部的锁资源成功
    6.清理dss内部的锁资源成功
    7.清理dss内部的锁资源成功
    8.合理报错
History     :
"""
import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.standby_node = Node('Standby1DbUser')
        self.common = Common()
        self.constant = Constant()

    def test_sharestorage_tool(self):
        step1 = "-----step1:清理dss内部的锁资源，不指定DSS_HOME参数;" \
                "expect:清理dss内部的锁资源成功-----"
        self.logger.info(step1)
        clean_cmd = f"source {macro.DB_ENV_PATH};dsscmd clean_vglock"
        self.logger.info(clean_cmd)
        res = self.common.get_sh_result(self.primary_node, clean_cmd)
        self.assertIn('Succeed to clean vg lock.', res, "执行失败" + step1)

        step2 = "-----step2:清理dss内部的锁资源，指定DSS_HOME参数为正常值;" \
                "expect:清理dss内部的锁资源成功-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd clean_vglock -D {macro.DSS_INSTANCE};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Succeed to clean vg lock.', execute_msg, "执行失败" + step2)

        step3 = "-----step3:清理dss内部的锁资源，指定错误的DSS_HOME参数;" \
                "expect:合理报错Failed to get vg entry info-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd clean_vglock -D {macro.DSS_INSTANCE}.cfg;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Failed to get vg entry info when alloc vg info',
                      execute_msg, "执行失败" + step3)
        self.assertIn('Failed to clean vg lock', execute_msg, "执行失败" + step3)

        step4 = "-----step4:清理dss内部的锁资源，指定权限不足的DSS_HOME参数;" \
                "expect:合理报错not exist-----"
        self.logger.info(step4)
        cmd = f"source {macro.DB_ENV_PATH};" \
            f"chmod 000 {macro.DSS_INSTANCE};" \
            f"dsscmd clean_vglock -D {macro.DSS_INSTANCE};" \
            f"chmod 700 {macro.DSS_INSTANCE}"
        self.logger.info(cmd)
        cmd_res = self.common.get_sh_result(self.primary_node, cmd)
        self.assertIn("Failed to get vg entry info when alloc vg info",
                      cmd_res, "执行失败" + step4)
        self.assertIn("dss_inst.ini does not exist", cmd_res, "执行失败" + step4)

        step5 = "-----step5:在备机点执行清理dss内部的锁资源;" \
                "expect:清理dss内部的锁资源成功-----"
        self.logger.info(step5)
        self.logger.info(clean_cmd)
        res = self.common.get_sh_result(self.standby_node, clean_cmd)
        self.assertIn('Succeed to clean vg lock', res, "执行失败" + step5)

        step6 = "-----step6:在dss关闭的情况下执行;expect:清理dss内部的锁资源成功-----"
        self.logger.info(step6)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd stopdss;sleep 2;" \
            f"dsscmd clean_vglock;" \
            f"dsscmd clean_vglock -D {macro.DSS_INSTANCE};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn('Succeed to stop server.', execute_msg, "执行失败" + step6)
        self.assertIn("Succeed to clean vg lock.", execute_msg, "执行失败" + step6)

        step7 = "-----step7:在集群停止的情况下执行;expect:清理dss内部的锁资源成功-----"
        self.logger.info(step7)
        try:
            stop_msg = self.primary_sh.exec_cm_ctl(mode='stop')
            self.logger.info(stop_msg)
            self.assertIn(self.constant.cm_stop_success_msg,
                          stop_msg, '执行失败' + step7)
            res = self.common.get_sh_result(self.primary_node, clean_cmd)
            self.assertIn('Succeed to clean vg lock.', res, "执行失败" + step7)
        finally:
            start_msg = self.primary_sh.exec_cm_ctl(mode='start')
            self.logger.info(start_msg)
            self.assertIn(self.constant.cm_start_success_msg,
                          start_msg, '启动集群失败')
            status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
            self.logger.info(status)
            self.assertIn('cluster_state   : Normal', status, '执行失败')

        step8 = "-----step8:DSS_HOME路径权限不足时，清理dss内部的锁资源，" \
                "不指定DSS_HOME参数;expect:合理报错-----"
        self.logger.info(step8)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"chmod 000 {macro.DSS_INSTANCE};" \
            f"dsscmd clean_vglock;" \
            f"chmod 700 {macro.DSS_INSTANCE}"
        self.logger.info(execute_cmd)
        res = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Failed to get vg entry info when alloc vg info",
                      res, "执行失败" + step8)
        self.assertIn("dss_inst.ini does not exist", res, "执行失败" + step8)

    def tearDown(self):
        self.logger.info("-----无需清理环境-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
